﻿using CBS.APP;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// EditCoursePlan.xaml 的交互逻辑
    /// </summary>
    public partial class EditCoursePlan : Window
    {
        private EditCoursePlanModel _coursePlan;
        private int _coursePlanId;
        private bool _isSemesterDataBound = false;
        private bool _isGradeDataBound = false;
        private bool _isClassDataBound = false;
        private bool _isCourseDataBound = false;
        private bool _isTeacherDataBound = false;

        public EditCoursePlan(EditCoursePlanModel model)
        {
            InitializeComponent();

            _coursePlan = model;
            this._coursePlanId = model.CoursePlanId;

            LoadCondation();

            tb_CoursePlan.Text = model.CoursePlanName;
            tb_CoursePlan.IsEnabled = false;
            tb_CourseCount.Text = model.CourseCount.ToString();
            tb_Long.Text = model.ClassTimeLong.ToString();
            tb_PlanNum.Text = model.PlanNum.ToString();
            tb_Memo.Text = model.Memo.ToString();
            tb_Price.Text = model.CoursePrice.ToString();
        }

        private async void LoadCondation()
        {
            // 加载 ComboBox 数据
            await LoadDataAsync();

            // 查询 Grid 数据
            //FlushGrid();
        }

        private async Task LoadDataAsync()
        {
            // 使用 Task.WhenAll 等待所有 ComboBox 数据加载完成
            var tasks = new List<Task>
            {
                BindSemester(),
                BindGrade(),
                BindClass(),
                BindCourse(),
                BindTeacher()
            };

            await Task.WhenAll(tasks);
        }

        private async Task BindSemester()
        {
            try
            {
                var list = await Task.Run(() => SemesterApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }


                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = list;
                }));
                int index = list.FindIndex(item => item.SemesterId == this._coursePlan.SemesterId);

                cb_Semester.SelectedIndex = index;


                // 设置标志为 true
                _isSemesterDataBound = true;
                cb_Semester.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async Task BindGrade()
        {
            try
            {
                var list = await Task.Run(() => GradeApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }


                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Grade.ItemsSource = list;
                }));

                int index = list.FindIndex(item => item.GradeId == this._coursePlan.GradeId);

                cb_Grade.SelectedIndex = index;

                // 设置标志为 true
                _isGradeDataBound = true;
                cb_Grade.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async Task BindClass()
        {
            try
            {
                var list = await Task.Run(() => ClassApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }


                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Class.ItemsSource = list;
                }));
                int index = list.FindIndex(item => item.ClassId == this._coursePlan.ClassId);

                cb_Class.SelectedIndex = index;



                // 设置标志为 true
                _isClassDataBound = true;
                cb_Class.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async Task BindCourse()
        {
            try
            {
                var list = await Task.Run(() => CourseApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }


                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Course.ItemsSource = list;
                }));
                int index = list.FindIndex(item => item.CourseId == this._coursePlan.CourseId);

                cb_Course.SelectedIndex = index;



                // 设置标志为 true
                _isCourseDataBound = true;
                cb_Course.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async Task BindTeacher()
        {
            try
            {
                var list = await Task.Run(() => TeacherApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }


                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Teacher.ItemsSource = list;
                }));
                int index = list.FindIndex(item => item.TeacherId == this._coursePlan.TeacherId);

                cb_Teacher.SelectedIndex = index;



                // 设置标志为 true
                _isTeacherDataBound = true;
                cb_Teacher.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CoursePlanViewModel coursePlan = new();
            //coursePlan.CoursePlanName = tb_CoursePlan.Text.Trim();
            coursePlan.CoursePrice = string.IsNullOrWhiteSpace(tb_Price.Text.Trim()) ? 0 : decimal.Parse(tb_Price.Text.Trim());
            coursePlan.ClassTimeLong = string.IsNullOrWhiteSpace(tb_Long.Text.Trim()) ? 0 : decimal.Parse(tb_Long.Text.Trim());
            //coursePlan.TeacherId = ((TeacherSelectModel)cb_Teacher.SelectedItem).TeacherId;
            //coursePlan.SemesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
            //coursePlan.GradeId = ((GradeViewModel)cb_Grade.SelectedItem).GradeId;
            //coursePlan.ClassId = ((ClassViewModel)cb_Class.SelectedItem).ClassId;
            //coursePlan.CourseId = ((CourseViewModel)cb_Course.SelectedItem).CourseId;
            coursePlan.Memo = tb_Memo.Text.Trim();
            coursePlan.CourseCount = string.IsNullOrWhiteSpace(tb_CourseCount.Text.Trim()) ? 0 : int.Parse(tb_CourseCount.Text.Trim());
            coursePlan.PlanNum = string.IsNullOrWhiteSpace(tb_PlanNum.Text.Trim()) ? 0 : int.Parse(tb_PlanNum.Text.Trim());

            try
            {
                if (CoursePlanApp.UpdateCoursePlan(_coursePlanId, coursePlan))
                {
                    this.DialogResult = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }
}
